package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import bean.JhshSearchBean;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Created by Ren on 2017/9/6.
 * 月计划审核 审核已完成 searchView
 * 暂时没有使用
 */

public class JhshYwcSearchView extends RelativeLayout implements View.OnClickListener {
    TextView jhnyks, jhnyjs;
    EditText yjhh, xm;
    AutoCompleteTextView dw;
    RadioButton zs, fzs;
    LinearLayout xm_layout;


    TextView lb, zt;
    Spinner spinner_lb, spinner_zt;

    /**
     * 按钮
     */
    TextView cancel, delect, confirm;

    JhshSearchBean mJhshSearchBean;
    JhshSearchView.OnClick mOnClick;

    private ListPopWindow ztListPopWindow;
    private ListPopWindow lbListPopWindow;
    private List ztlist = new ArrayList();
    private List lblist = new ArrayList();

    public JhshYwcSearchView(Context context) {
        super(context);
        initView();
        initAutoCompleteTextView();
        initListener();
    }

    public JhshYwcSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initAutoCompleteTextView();
        initListener();
    }

    public JhshYwcSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAutoCompleteTextView();
        initListener();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.jhshsearchview, this);
        jhnyks = (TextView) findViewById(R.id.jhnyks);
        jhnyjs = (TextView) findViewById(R.id.jhnyjs);
        yjhh = (EditText) findViewById(R.id.yjhh);
        xm = (EditText) findViewById(R.id.xm);
        dw = (AutoCompleteTextView) findViewById(R.id.dwhbm);
        lb = (TextView) findViewById(R.id.lb);
        spinner_lb = (Spinner) findViewById(R.id.spinner_lb);
        zt = (TextView) findViewById(R.id.zt);
        spinner_zt = (Spinner) findViewById(R.id.spinner_zt);
        zs = (RadioButton) findViewById(R.id.zs);
        fzs = (RadioButton) findViewById(R.id.fzs);
        xm_layout = (LinearLayout) findViewById(R.id.xm_layout);


        cancel = (TextView) findViewById(R.id.cancel);
        delect = (TextView) findViewById(R.id.delect);
        confirm = (TextView) findViewById(R.id.confirm);


        if (mJhshSearchBean == null) {
            mJhshSearchBean = new JhshSearchBean();
        }

    }

    private void initAutoCompleteTextView() {
        ztlist = Arrays.asList(getContext().getResources().getStringArray(R.array.yjhywc_zt));
        lblist = Arrays.asList(getContext().getResources().getStringArray(R.array.lb));
        dw.setAdapter(new ArrayAdapter<UnitInformation>(getContext(), android.R.layout.simple_list_item_1, DictionariesUtil.getUnitList()));
        zt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ztListPopWindow = new ListPopWindow.Builder(getContext()).setList(ztlist)
                        .setParentView(zt)
                        .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                            @Override
                            public void OnListItemCilck(int position) {
                                zt.setText(ztlist.get(position).toString());
                                ztListPopWindow.dismiss();
                            }
                        }).builder();
                ztListPopWindow.show();
            }
        });

        lb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                lbListPopWindow = new ListPopWindow.Builder(getContext()).setList(lblist)
                        .setParentView(zt)
                        .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                            @Override
                            public void OnListItemCilck(int position) {
                                lb.setText(lblist.get(position).toString());
                                lbListPopWindow.dismiss();
                            }
                        }).builder();
                lbListPopWindow.show();
            }
        });

    }


    private void initListener() {
        jhnyks.setOnClickListener(this);
        jhnyjs.setOnClickListener(this);
        cancel.setOnClickListener(this);
        delect.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.jhnyjs:
                TimePickerView.getInstanceEndMonth(getContext(), new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        jhnyjs.setText(getTime(date));
                    }
                }).show();
                break;
            case R.id.jhnyks:
                TimePickerView.getInstanceEndMonth(getContext(), new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        jhnyks.setText(getTime(date));
                    }
                }).show();
                break;
            case R.id.cancel:
                setVisibility(GONE);
                break;
            case R.id.delect:
                jhnyks.setText("");
                jhnyjs.setText("");
                yjhh.setText("");
                dw.setText("");
                xm.setText("");
                lb.setText("");
                zt.setText("");
                mJhshSearchBean.clear();
                break;
            case R.id.confirm:
                mJhshSearchBean.setJhh(yjhh.getText().toString());
                mJhshSearchBean.setJhnyks(jhnyks.getText().toString());
                mJhshSearchBean.setJhnyjs(jhnyjs.getText().toString());
                mJhshSearchBean.setLb(spinner_lb.getSelectedItem().toString());
                mJhshSearchBean.setXm(xm.getText().toString());
                List<UnitInformation> list = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                        .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(dw.getText().toString())).list();
                if (list.size() != 0) {
                    mJhshSearchBean.setDwhbmId(list.get(0).getLjyyxDwxxId());
                } else {
                    mJhshSearchBean.setDwhbmId("-1");
                }
                if (spinner_zt.getSelectedItem().toString().equals("可审核")) {
                    mJhshSearchBean.setZt("2");
                }
                if (spinner_zt.getSelectedItem().toString().equals("待审核")) {
                    mJhshSearchBean.setZt("3");
                }
                if (spinner_zt.getSelectedItem().toString().equals("已审批")) {
                    mJhshSearchBean.setZt("4");
                }
                if (spinner_zt.getSelectedItem().toString().equals("已超时")) {
                    mJhshSearchBean.setZt("5");
                }
                if (zs.isChecked()) {
                    mJhshSearchBean.setSfzs("主审");
                }
                if (fzs.isChecked()) {
                    mJhshSearchBean.setSfzs("非主审");
                }
                if (mOnClick != null) {
                    mOnClick.onConfirmListener(mJhshSearchBean);
                }
                setVisibility(GONE);
                break;
        }
    }

    public void hideXmLayout() {
        xm_layout.setVisibility(GONE);
    }

    public void setConfirmListener(JhshSearchView.OnClick onClick) {
        this.mOnClick = onClick;
    }

    public interface OnClick {
        void onConfirmListener(JhshSearchBean Util);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }
}
